package com.example.app2024.toolbar;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app2024.R;

public class ToolbarActivity extends AppCompatActivity
{
      Toolbar toolbar;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_toolbar);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.maintool), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            toolbar = findViewById(R.id.toolbar);
            toolbar.setLogo(R.drawable.pic001);
            toolbar.setTitle("标题");
            toolbar.setSubtitle("子标题");

            this.setSupportActionBar(toolbar);

            toolbar.setNavigationIcon(R.drawable.pic003);
            toolbar.setNavigationOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        finish();
                  }
            });

            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
            {
                  @Override
                  public boolean onMenuItemClick(MenuItem item)
                  {
                        int id = item.getItemId();
                        Toast.makeText(ToolbarActivity.this, "你占击的菜单id :" + id, Toast.LENGTH_LONG).show();
                        return true;
                  }
            });
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      {
            getMenuInflater().inflate(R.menu.menuitem, menu);
            return true;
      }
}